package controller.book;

import entity.Book;
import entity.User;
import util.JdbcDeal;

import javax.swing.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Book_del extends JPanel {

    public Book_del(User user) {
        try {
            String table_Gno = JOptionPane.showInputDialog("请输入要删除货物编号）");
            Book book = new Book();
            book.setBookNo(table_Gno);
            String sql = "SELECT * FROM book WHERE Bno = '" + table_Gno + "'";
            JdbcDeal jdbcDeal = new JdbcDeal();
            ResultSet resultSet = jdbcDeal.select(sql);
            if(resultSet.next()){
                book.setGno(resultSet.getString("Gno"));
                book.setBnum(Integer.parseInt(resultSet.getString("Bnum")));
                book.setWno(resultSet.getString("Wno"));
            }
            if(user.isFlag()==true || book.getWno() == user.getWorkNo()){
                if (jdbcDeal.Bookdel(book) == 1) {
                    JOptionPane.showMessageDialog(this, "成功删除一条记录！", "", JOptionPane.PLAIN_MESSAGE);
                    return;
                }
            }else {
                JOptionPane.showMessageDialog(this, "删除记录失败！可能和其他信息有联动！请先删除相关信息！", "", JOptionPane.WARNING_MESSAGE);
                return;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
